package com.groupbuying.settleconsumer.service.impl;

import com.groupbuying.settleconsumer.constants.SettleWithdrawalEnumConstants;
import com.groupbuying.settleconsumer.dao.SettleUshopWithdrawalReceiptDao;
import com.groupbuying.settleconsumer.domain.SettleUshopWithdrawalReceiptDO;
import com.groupbuying.settleconsumer.dto.YSWithdrawalReturnDTO;
import com.groupbuying.settleconsumer.service.SettleUshopYSWithdrawalReceiptService;
import com.groupbuying.settleconsumer.utils.JsonUtils;
import com.groupbuying.settleconsumer.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class SettleUshopYSWithdrawalReceiptServiceImpl implements SettleUshopYSWithdrawalReceiptService {
	public static final Logger logger = LoggerFactory.getLogger(SettleUshopYSWithdrawalReceiptServiceImpl.class);
	@Autowired
	private SettleUshopWithdrawalReceiptDao settleWithdrawalReceiptDao;
	
	@Override
	public SettleUshopWithdrawalReceiptDO get(Integer withdrawalreceiptId){
		return settleWithdrawalReceiptDao.get(withdrawalreceiptId);
	}
	
	@Override
	public List<SettleUshopWithdrawalReceiptDO> list(Map<String, Object> map){
		return settleWithdrawalReceiptDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return settleWithdrawalReceiptDao.count(map);
	}
	
	@Override
	public int save(SettleUshopWithdrawalReceiptDO settleWithdrawalReceipt){
		return settleWithdrawalReceiptDao.save(settleWithdrawalReceipt);
	}
	
	@Override
	public int update(SettleUshopWithdrawalReceiptDO settleWithdrawalReceipt){
		return settleWithdrawalReceiptDao.update(settleWithdrawalReceipt);
	}
	
	@Override
	public int remove(Integer withdrawalreceiptId){
		return settleWithdrawalReceiptDao.remove(withdrawalreceiptId);
	}
	
	@Override
	public int batchRemove(Integer[] withdrawalreceiptIds){
		return settleWithdrawalReceiptDao.batchRemove(withdrawalreceiptIds);
	}
	/**银盛提现异步回执
	 * @param withdrawalReceipt
	 * @return com.groupbuying.common.apiresult.ApiResult
	 */
	@Override
	@Transactional
	public ApiResult createWithdrawalReceipt(String withdrawalReceipt) {
		try {
            logger.error("SettleUshopYSWithdrawalReceiptServiceImpl.createWithdrawalReceipt()-----银盛提现异步回执json为："+withdrawalReceipt);
            SettleUshopWithdrawalReceiptDO settleWithdrawalReceiptDO = new SettleUshopWithdrawalReceiptDO();
			/*银盛同步返回信息*/
			Map<String ,Object> condMap=JsonUtils.jsonToMap(withdrawalReceipt);
			if (condMap != null&& condMap.size()>0) {
				/*解析参数*/
				String accountDate = (String)condMap.get("account_date");
				String fee = (String)condMap.get("fee");
				String notifyTime = (String)condMap.get("notify_time");
				String outTradeNo = (String)condMap.get("out_trade_no");
				String partnerFee =(String)condMap.get("partner_fee");
				String payeeFee = (String)condMap.get("payee_fee");
				String totalAmount = (String)condMap.get("total_amount");
				String tradeNo =  (String)condMap.get("trade_no");
				String tradeStatus = (String)condMap.get("trade_status");
				String tradeStatusDescription = (String)condMap.get("trade_status_description");
				String payerFee =(String)condMap.get("payer_fee");
				String signType = (String)condMap.get("sign_type");
				String notifyTyp = (String)condMap.get("notify_type");
				if (StringUtils.isNotEmpty(outTradeNo) && StringUtils.isNotEmpty(tradeStatus)&& StringUtils.isNotEmpty(accountDate) && StringUtils.isNotEmpty(totalAmount) && StringUtils.isNotEmpty(partnerFee)
						&& StringUtils.isNotEmpty(payeeFee) && StringUtils.isNotEmpty(payerFee)&&StringUtils.isNotEmpty(notifyTime)&&StringUtils.isNotEmpty(notifyTyp)&&StringUtils.isNotEmpty(signType)) {
					settleWithdrawalReceiptDO.setAccountDate(accountDate);
					settleWithdrawalReceiptDO.setFee(new BigDecimal(fee));
					settleWithdrawalReceiptDO.setNotifyTime(notifyTime);
					settleWithdrawalReceiptDO.setNotifyType(notifyTyp);
					settleWithdrawalReceiptDO.setOutTradeNo(outTradeNo);
					settleWithdrawalReceiptDO.setPartnerFee(new BigDecimal(partnerFee));
					settleWithdrawalReceiptDO.setPayerFee(new BigDecimal(payerFee));
					settleWithdrawalReceiptDO.setPayeeFee(new BigDecimal(payeeFee));
					settleWithdrawalReceiptDO.setSign(SettleWithdrawalEnumConstants.ENUM_SIGN_SUCCESS);
					settleWithdrawalReceiptDO.setSignType(signType);
					settleWithdrawalReceiptDO.setTradeStatusDescription(tradeStatusDescription);
					settleWithdrawalReceiptDO.setTradeStatus(tradeStatus);
					settleWithdrawalReceiptDO.setTradeNo(tradeNo);
					settleWithdrawalReceiptDO.setTotalAmount(new BigDecimal(totalAmount));
					settleWithdrawalReceiptDO.setGmtCreate(new Date());
					settleWithdrawalReceiptDO.setGmtModified(new Date());
					settleWithdrawalReceiptDO.setIsDelete(SettleWithdrawalEnumConstants.ENUM_ISDELETE_UNDEL);
					this.save(settleWithdrawalReceiptDO);
                    logger.error("SettleUshopYSWithdrawalReceiptServiceImpl.createWithdrawalReceipt()-----银盛提现异步回执成功落库success！！！");
					return ApiResult.success();
				} else {
					logger.error("SettleUshopYSWithdrawalReceiptServiceImpl.createWithdrawalReceipt()-----银盛提现异步回执失败落库fail！！！");
					return ApiResult.failure();
				}
			} else {
				logger.error("SettleUshopYSWithdrawalReceiptServiceImpl.createWithdrawalReceipt()------获取银盛提现异步参数为空");
				return ApiResult.failure("获取银盛提现异步参数为空!");
			}
		} catch (Exception e) {
			e.printStackTrace();
            logger.error("SettleUshopYSWithdrawalReceiptServiceImpl.createWithdrawalReceipt()-----银盛提现异步回执异常！！！"+e);
		}
		return ApiResult.failure();
	}

}
